<template>
  <page-header-wrapper>
    <a-card title="消息列表">
      <template #extra>
        <a-tooltip>
          <template #title> 数据导出</template>
          <a-button type="primary" icon="database" @click="handleExpert(queryParam)"></a-button>
        </a-tooltip>
      </template>
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="筛选">
                <a-input v-model="queryParam.keyword" placeholder="请输入" />
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" :loading="loading" @click="handleSearch">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
 
      <div class="table-operator">
        <a-button type="primary" @click="handleRead()" :loading="readLoading">全部已读</a-button>
        <a-button type="primary" @click="handleEmpty()" :loading="emptyLoading">清空消息</a-button>
        <!-- <a-button icon="cloud-upload">导出</a-button> -->
      </div>

      <a-table
        size="default"
        rowKey="key"
        :columns="columns"
        :data-source="loadData"
        :pagination="pagination"
        @change="handleTableChange"
      >
        <span slot="msgCls" slot-scope="text, record">
          <template>
            <div>{{ record.msgCls == '1' ? '售后提醒' : '' }}</div>
          </template>
        </span>
        <span slot="msgStatus" slot-scope="text, record">
          <template>
            <a-tag color="#82af6f" v-if="record?.msgStatus == '1'">已读</a-tag>
            <a-tag color="#D15B47" v-if="record?.msgStatus == '0'">未读</a-tag>
          </template>
        </span>
        <span slot="msgUrl" slot-scope="text, record">
          <template>
            <a-tag color="#428bca" @click="handle(record)"><a-icon type="form" /></a-tag></template>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a-tag color="#428bca" @click="handleDetail(record)">详情</a-tag>
          </template>
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
// import moment from 'moment'
import { Ellipsis } from '@/components'
import { myMsgListUsingPOST, allReadMyMsgUsingPOST, deleteAllMyMsgUsingPOST,exportMymsgUsingPOST } from '@/api/api/wode.ts'
const columns = [
  {
    title: '时间',
    dataIndex: 'msgTime',
  },
  {
    title: '类型',
    dataIndex: 'msgCls',
    scopedSlots: { customRender: 'msgCls' },
    // customRender: (val) => val.status
  },
  {
    title: '标题',
    dataIndex: 'msgTitle',
  },
  {
    title: '内容',
    dataIndex: 'msgContent',
  },
  {
    title: '前往',
    dataIndex: 'msgUrl',
    scopedSlots: { customRender: 'msgUrl' },
  },
  {
    title: '状态',
    dataIndex: 'msgStatus',
    scopedSlots: { customRender: 'msgStatus' },
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: { customRender: 'action' },
  },
]
export default {
  name: 'Receiptpay',
  components: {
    Ellipsis,
  },
  data() {
    this.columns = columns
    return {
      // create model
      visible: false,
      emptyLoading: false,
      readLoading: false,
      confirmLoading: false,
      mdl: null,
      loading: false,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
        pageNo:1,
        pageSize:10,
        queryCondition:'',
        status:''
      },
      pagination: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: [],
    }
  },
  created() {
    this.handleSearch()
  },
  methods: {
    handleExpert(params){
      exportMymsgUsingPOST({ ...params}).then((res) => {
          const blob = new Blob([res], {
            type: 'application/vnd.ms-excel;charset=utf-8',
          })
          const filename = '消息列表' + '.xlsx'
          // 将blob对象转为一个URL
          var blobURL = window.URL.createObjectURL(blob)
          // 创建一个a标签
          var tempLink = document.createElement('a')
          // 隐藏a标签
          tempLink.style.display = 'none'
          // 设置a标签的href属性为blob对象转化的URL
          tempLink.href = blobURL
          // 给a标签添加下载属性
          tempLink.setAttribute('download', filename)
          if (typeof tempLink.download === 'undefined') {
            tempLink.setAttribute('target', '_blank')
          }
          // 将a标签添加到body当中
          document.body.appendChild(tempLink)
          // 启动下载
          tempLink.click()
          // 下载完毕删除a标签
          document.body.removeChild(tempLink)
          window.URL.revokeObjectURL(blobURL)
          this.$message.success('导出成功~')
        })  
    },
    handle(record) {
      this.$router.push({ path: `/service/service/detail/${record.msgUrl}` })
    },
    handleDetail(record) {
      this.$router.push({ path: `/my/message/detail/${record.id}` })
    },
    handleRead() {
      this.readLoading = true
      allReadMyMsgUsingPOST()
        .then(() => {
          this.handleSearch()
          this.readLoading = false
        })
        .catch((error) => {
          this.readLoading = false
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleEmpty() {
      this.emptyLoading = true
      deleteAllMyMsgUsingPOST()
        .then(() => {
          this.handleSearch()
          this.emptyLoading = false
        })
        .catch((error) => {
          this.emptyLoading = false
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleSearch(params) {
      this.loading = true
      myMsgListUsingPOST({...params,pageSize:this.pagination.pageSize,pageNo:this.pagination.current})
        .then((res) => {
          this.loading = false
          this.loadData = res.data.list
          this.queryParam.pageNo = this.pagination.current
          this.queryParam.pageSize = this.pagination.pageSize
          this.pagination = { ...this.pagination, total: Number(res.data.total) }
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleTableChange(pagination, filters, sorter) {
      const pager = { ...this.pagination }
      pager.current = pagination.current
      pager.pageSize = pagination.pageSize
      this.pagination = pager
      this.handleSearch({
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...filters,
        pageNo: pagination.current,
        pageSize: pagination.pageSize,
        queryCondition: this.queryParam.queryCondition,
        status: this.queryParam.status,
        date: this.queryParam.date,
      })
    },
    handleAdd() {
      this.mdl = null
      this.visible = true
    },
  },
}
</script>